var prefix = jsctx + "/tenant/serviceType";

var tenantSms = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };
    function getcount() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/findTenantSms",
            async: false,
            success: function (data) {
                $("#tenantCode").val(data.data.tenantCode)
                $("#amount").val(data.data.amount)
                $("#usedAmout").val(data.data.usedAmout)
                $("#unitPrice").val(data.data.unitPrice)

            }
        });
    }

    function init() {
        getcount();
        load();
    };

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/findTenantSmsList",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                sidePagination: "server",
                cache:false,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        beginTime: $("#beginTime").val().trim(),
                        endTime: $("#endTime").val().trim(),
                    };
                },
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        width:'100',align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {field: 'memberName', title: '会员名称', align: 'center'},
                    {field: 'memberPhone', title: '会员电话', align: 'center'},
                    {
                        field: 'recordType', title: '记录类型', width:'100',align: 'center',
                        formatter: function (value, row, index) {
                            if (row.recordType == '0') {
                                return '<span >通知类</span>';
                            } else if (row.recordType == '1') {
                                return '<span>推广类</span>';
                            }
                        }
                    },
                    {field: 'createdTime', title: '创建时间', align: 'center'},
                    {field: 'remark', title: '备注', align: 'center'},
                ]
            });
    };


    function exportSmsRecord() {
        var beginTime = $("#beginTime").val().trim(),
            endTime = $("#endTime").val().trim(),
            count = $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),

            offset = $('.active .page-link').text(),
            limit = $(".page-size").text();

        location.href = prefix + "/exportSmsRecord?beginTime=" + beginTime + "&endTime=" + endTime
            + "&count=" + count + "&offset=" + offset + "&limit=" + limit;
    }

    return {
        init: init,
        reLoad: reLoad,
        getcount:getcount,
        exportSmsRecord:exportSmsRecord

    };
})();
